package com.xm2.demo.login.admin.controller;

import com.xm2.demo.login.admin.service.AdminService;
import com.xm2.demo.login.user.vo.LoginVo;
import com.xm2.demo.login.user.vo.UpdateVo;
import com.xm2.demo.model.Position;
import com.xm2.demo.model.User;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Create
 * @create 2018-12-20 17:37
 * @Description：
 */
@Controller
@RequestMapping(value = "/system/")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping(value = "login")
    public String login() {

        return "admin";
    }

    @PostMapping(value = "login")
    @ResponseBody
    public JsonResult doLogin(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        if (loginVo.getUsername().equals("")) {
            return new JsonResult("101", "用户名不能为空!");
        } else if (loginVo.getPassword().equals("")) {
            return new JsonResult("102", "密码不能为空!");
        } else {
            //根据用户名查询
            User user = adminService.findUserByPhone(loginVo.getUsername());
            if (user != null) {
                if (user.getPassword().equals(Md5.getMD5(loginVo.getPassword()))) {
                    Position position = adminService.getStateArr(user);
                    request.getSession().setAttribute("admin", user);
                    request.getSession().setAttribute("position", position);
                    if (user.getPositionId() == 1) {
                        return new JsonResult("/system/main");
                    } else {

                        return new JsonResult("/system/order");
                    }

                } else {
                    return new JsonResult("103", "用户名或密码错误!");
                }
            }
            return new JsonResult("103", "用户名或密码错误!");
        }
    }

    @GetMapping(value = "logout")
    public String logout(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("admin");
        if (user != null) {
            request.getSession().invalidate();
        }
        return "redirect:/system/login";
    }

    @PostMapping(value = "update")
    @ResponseBody
    public JsonResult updatePassword(@RequestBody UpdateVo updateVo) {
        if (updateVo.getUserNew().equals("")) {
            return new JsonResult("101", "用户名不能为空!");
        } else if (updateVo.getPassOld().equals("")) {
            return new JsonResult("102", "原密码不能为空!");
        } else if (updateVo.getPassNew().equals("")) {
            return new JsonResult("102", "新密码不能为空!");
        } else {
            User user = adminService.findUserByPhone(updateVo.getUserNew());
            if (user == null) {
                return new JsonResult("104", "用户名不存在!");
            } else {
                if (!user.getPassword().equals(Md5.getMD5(updateVo.getPassOld()))) {
                    return new JsonResult("105", "用户名或密码错误!");
                } else {
                    //修改密码
                    user.setPassword(Md5.getMD5(updateVo.getPassNew()));
                    adminService.updatePassword(user);
                    return new JsonResult();
                }
            }
        }
    }
}
